﻿import datetime
import time
from google.appengine.ext import db

class Tag(db.Model):
    name = db.StringProperty(multiline=False)
    def entries(self):
        return (x.entry for x in self.entrytag_set)
    def id(self):
        return self.key().id()
    def entriescount(self):
        return self.entrytag_set.count()

class Category(db.Model):
    name = db.StringProperty(required=True)
    imgname = db.StringProperty(required=True)
    def entries(self):
        return self.entry_set.order('-pub_date')
    def id(self):
        return self.key().id()
    def entriescount(self):
        return self.entry_set.count()

class Entry(db.Model):
    title = db.StringProperty(required=True)
    #titleurl = db.StringProperty(required=False)
    excerpt = db.TextProperty()
    content = db.TextProperty()
    category = db.ReferenceProperty(Category)
    accesscount = db.IntegerProperty(default=0)
    pub_date = db.DateTimeProperty(auto_now_add=True)
    def tags(self):
        return (x.tag for x in self.entrytag_set)
    def comments(self):
        return self.comment_set.order('-pub_date')
    @property
    def id(self):
        return self.key().id()
    def commentscount(self):
        return self.comment_set.count()
    def pub_date_str(self):
        return self.pub_date.strftime("%Y-%m-%d %H:%M:%S")

class Comment(db.Model):
    user_name = db.StringProperty()
    user_mail = db.EmailProperty()
    content = db.TextProperty(required = True)
    entry = db.ReferenceProperty(Entry)
    pub_date = db.DateTimeProperty(auto_now_add = True)
    def id(self):
        return self.key().id()
    def pub_date_str(self):
        return self.pub_date.strftime("%Y-%m-%d %H:%M:%S")

class EntryTag(db.Model):
    entry = db.ReferenceProperty(Entry)
    tag = db.ReferenceProperty(Tag)
    pub_date = db.DateTimeProperty(required=True)